<?php
/**
 * CK 开发框架
 * User: Clake
 * Date: 16/9/28
 * Time: 17:51
 */

namespace CK\Util;

require_once CK_PATH.'/outvendor/alidayu/TopSdk.php';

use CK\Core\Component;

class DayuSms extends Component{
    private $appkey = '23395889';
    //大鱼 secretKey
    private $secretKey = 'b201f1d0233e2a98cb33cdc36406a6b4';

    public function __construct($conf=null) {
        if ($conf === null) {
            $conf = $this->getConfig();
        }

        if ($conf) {
            $this->appkey = $conf['app_key'];
            $this->secretKey = $conf['secret_key'];
        }
    }

    /**
     * 设置发送时的APPKEY和SECRETKEY
     * @param $appkey
     * @param $secret
     */
    public function setKey($appkey,$secret) {
        $this->appkey = $appkey;
        $this->secretKey = $secret;
    }

    /**
     * 发送默认验证码短信
     * @param string $mobile 手机号码
     * @param string $code
     * @param string $product_name
     *
     * @return array|bool
     */
    public function sendSignCode($mobile,$code,$product_name) {
        $params = [
            'code'=>$code,
            'product'=>$product_name
        ];

        return self::send($mobile, $params,'身份验证' ,'SMS_11025904');
    }

    /**
     * 发送大鱼测试短信
     * @param string $mobile        手机号码
     * @param string $customer      用户名
     *
     * @return array|bool
     */
    public function sendTest($mobile,$customer) {
        $params = [
            'customer'=>$customer
        ];

        return self::send($mobile, $params,'大鱼测试' ,'SMS_11025903');
    }

    /**
     * 发送短信
     * @param string $mobile        手机号
     * @param string $params        发送参数
     * @param string $sign_name     发送签名
     * @param string $template      发送模板ID
     * @param string $ext           附加信息
     *
     * @return array|bool 返回调用结果
     */
    public function send($mobile,$params,$sign_name,$template,$ext='') {
        $c = new \TopClient;
        $c->appkey = $this->appkey;
        $c->secretKey = $this->secretKey;
        $c->format = 'json';
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend($ext);
        $req->setSmsType("normal");
        $req->setSmsFreeSignName($sign_name);
        $req->setSmsParam(json_encode($params));
        $req->setRecNum($mobile);
        $req->setSmsTemplateCode($template);
        $resp = $c->execute($req);
        $resp = (array)$resp;
        if (isset($resp['result']) && $resp['result']->err_code === '0' && $resp['result']->success) {
            return true;
        } else {
            return $resp;
        }
    }
}